package com.yige.service.mongoService

import com.mongodb.BasicDBObject
import com.mongodb.DBCollection
import com.mongodb.DBObject
import com.yige.common.SystemConstant
import com.yige.service.TeacherService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * Created by sunhao on 2015/9/17 0017.
 */

@Service
class MongoTeacherService extends BasicMongoServiceImp implements TeacherService{

    @Autowired
    MongoLessonService lessonService
    @Autowired
    MongoStudentService studentService



    DBCollection getCollection(){
        return mongoTemplate.getCollection(SystemConstant.MONGODB_COL_TEACHER)
    }

    @Override
    List<DBObject> findAllLessonForTeacher(Object teacherId) {
        DBObject teacher = findOneById(teacherId)
        List<DBObject> lessonList = teacher.teach
        return lessonList
    }

    @Override
    boolean bingLessonForTeacher(Object teacherId, Object lessonId) {
        List<DBObject> lessons = findAllLessonForTeacher(teacherId)
        lessons.add(lessonId)
        updateLessonForTeacher(teacherId,lessons)
        return true
    }

    @Override
    boolean removeLessonForTeacher(Object teacherId, Object lessonId) {
        List<DBObject> lessons = findAllLessonForTeacher(teacherId)
        lessons.remove(lessonId)
        updateLessonForTeacher(teacherId,lessons)
        return true
    }

    @Override
    boolean updateLessonForTeacher(Object teacherId, List<DBObject> lessons) {
        return getCollection().update(
                [_id:teacherId]as BasicDBObject,[$set:[teach:lessons]as BasicDBObject]as BasicDBObject
        )
    }

    @Override
    List<DBObject> findAllStudentForTeacher(Object teacherId) {
        DBObject teacher = findOneById(teacherId)
        List<DBObject> studentList = teacher.students
        return studentList
    }

    @Override
    boolean bindStudentForTeacher(Object teacherId, Object studentId) {
        List<DBObject> students = findAllStudentForTeacher(teacherId)
        students.add(studentId)
        updateStudentForTeacher(teacherId,students)
        return true
    }

    @Override
    boolean removeStudentForTeacher(Object teacherId, Object studentId) {
        List<DBObject> students = findAllStudentForTeacher(teacherId)
        students.remove(studentId)
        updateStudentForTeacher(teacherId,students)
        return true
    }

    @Override
    boolean updateStudentForTeacher(Object teacherId, List<DBObject> students) {
        return getCollection().update(
                [_id:teacherId]as BasicDBObject,[$set:[students:students]as BasicDBObject]as BasicDBObject
        )
    }

    @Override
    List<DBObject> findSalaryForTeacher(Object teacherId) {
        DBObject teacher = findOneById(teacherId)
        List<DBObject> salaryList = teacher.salary
        return salaryList
    }

    @Override
    boolean addSalaryForTeacher(Object teacherId, Object salaryId) {
        List<DBObject> salaryList = findSalaryForTeacher(teacherId)
        salaryList.add(salaryId)
        updateSalaryForTeacher(teacherId,salaryList)
        return true
    }

    @Override
    boolean removeSalaryForTeacher(Object teacherId, Object salaryId) {
        List<DBObject> salaryList = findSalaryForTeacher(teacherId)
        salaryList.remove(salaryId)
        updateSalaryForTeacher(teacherId,salaryList)
        return true
    }

    @Override
    boolean updateSalaryForTeacher(Object teacherId, List<DBObject> salaryList) {
        return getCollection().update(
                [_id:teacherId]as BasicDBObject,[$set:[salary:salaryList]as BasicDBObject]as BasicDBObject
        )
    }

    @Override
    boolean insertTeachInfo(Object teacherId, Set<String> teachInfo) {
        def list = new LinkedList(), teacher_db = findOneById(teacherId)
        teachInfo.each {
            list.add(teacher_db.name + "在" + it + "出勤教学")
        }
        getCollection().update([_id:teacherId]as BasicDBObject,[teachInfo:list]as BasicDBObject,true,false)
        return true
    }
}
